package com.regan.test;

import cn.hutool.core.util.StrUtil;
import com.google.common.collect.Lists;
import com.jfinal.kit.StrKit;
import com.regan.erp.util.CommonUtil;
import com.regan.erp.util.FunctionUtils;

import java.util.List;
import java.util.function.Function;

/**
 * 解析sql
 *
 * @author zhangby
 * @date 2018/9/13 下午8:25
 */
public class ParseSql {

    /**
     * ┌─┐       ┌─┐
     * ┌──┘ ┴───────┘ ┴──┐
     * │                 │
     * │       ───       │
     * │  ─┬┘       └┬─  │
     * │                 │
     * │       ─┴─       │
     * │                 │
     * └───┐         ┌───┘
     * │         │
     * │         │
     * │         │
     * │         └──────────────┐
     * │                        │
     * │                        ├─┐
     * │                        ┌─┘
     * │                        │
     * └─┐  ┐  ┌───────┬──┐  ┌──┘
     * │ ─┤ ─┤       │ ─┤ ─┤
     * └──┴──┘       └──┴──┘
     * 神兽保佑
     * 代码无BUG!
     */

    /*
           解析

          `user_name` varchar(100) DEFAULT NULL COMMENT '登录用户名',
          `password` varchar(100) DEFAULT NULL COMMENT '密码',
          `nick_name` varchar(100) DEFAULT NULL COMMENT '名称',
          `head_img` varchar(225) DEFAULT NULL COMMENT '头像',

     */
    public static void main(String[] args) {
        String str = " `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '角色ID',\n" +
                "  `name` varchar(100) NOT NULL COMMENT '角色名称',\n" +
                "  `auth_key` varchar(100) NOT NULL COMMENT '授权标识',\n" +
                "  `sort` int(4) DEFAULT '10' COMMENT '显示顺序',\n" +
                "  `status` char(1) NOT NULL DEFAULT '0' COMMENT '角色状态（0正常 1停用）',\n" +
                "  `create_user_id` varchar(64) DEFAULT '' COMMENT '创建者',\n" +
                "  `create_date` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',\n" +
                "  `update_user_id` varchar(64) DEFAULT '' COMMENT '更新者',\n" +
                "  `update_date` datetime DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',\n" +
                "  `remarks` varchar(500) DEFAULT '' COMMENT '备注',\n" +
                "  `del_flag` char(1) DEFAULT '0' COMMENT '删除标记  0：正常，1：删除',";
        List<String> strings = CommonUtil.splitStr(str, ",");
        List<List<String>> convers = FunctionUtils.convers(strings, newFun);

        // @resqParam value|地区Id|String|必填
        String strParamTemp = "@param {}|{}|{}|{}";
        convers.forEach(s -> {
            String rs = StrUtil.format(strParamTemp,
                    s.get(0),
                    s.size() > 3 ? s.get(3) : "NULL",
                    s.get(1).contains("varchar") ? "String" :
                            s.get(1).contains("timestamp") ? "date" : s.get(1),
                    s.get(2).contains("NOT NULL") ? "必填" : "非必填"
            );
            System.out.println(rs);
        });

        System.out.println();
        System.out.println(" ------------------------------ ");
        System.out.println();

        // @resqParam value|地区Id|String|必填
        String strTemp = "@resqParam {}|{}|{}|{}";
        convers.forEach(s -> {
            String rs = StrUtil.format(strTemp,
                    s.get(0),
                    s.size() > 3 ? s.get(3) : "NULL",
                    s.get(1).contains("varchar") ? "String" :
                            s.get(1).contains("timestamp") ? "date" : s.get(1),
                    s.get(2).contains("NOT NULL") ? "必填" : "非必填"
            );
            System.out.println(rs);
        });
    }

    static Function<String, List<String>> fun = str -> {
        String temp = "`{}` {}({}) {}'{}'";
        List<String> strings = CommonUtil.splitStr4Temp(str, temp);
        if (strings.size() == 3){
            String ntemp = "`{}` {} DEFAULT {} '{}'";
            strings = CommonUtil.splitStr4Temp(str, ntemp);
        }
        if (strings.size() > 2) {
            strings.remove(2);
        }
        return strings;
    };

    static Function<String, List<String>> newFun = str -> {
        String temp = "{} COMMENT {}";
        List<String> strings = CommonUtil.splitStr4Temp(str, temp);
        List<String> strings1 = Lists.newArrayList();
        strings1 = CommonUtil.splitStr4Temp(strings.get(0), "`{}` {}({}) {}");
        if (strings1.size() > 2) {
            strings1.remove(2);
        } else {
            strings1 = CommonUtil.splitStr4Temp(strings.get(0), "`{}` {} NULL {}");
        }
        List<String> rsList = Lists.newArrayList();
        rsList.addAll(strings1);
        if (strings.size() > 1) {
            rsList.add(strings.get(1));
        }
        return rsList;
    };


}
